﻿function loadMealPlan(paging, pageno, userid, user,isSearch) {
    var url = "";
    var data = "";
    url = "PIMService.asmx/GetMealPlanByNamePagin";
    data = "{'MealPlanName':'" + $('.suggest').val() + "','UserId':'" + userid + "','page_no':'" + pageno + "','page_lenght':'" + paging + "'}";

    $.ajax({
        type: "POST",
        url: url,
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            if (response.d.length) {
                BindMealPlan(response, userid, user);
            }
            else {
                $('#loading').hide();
                var divbg = document.getElementById('bg');
                divbg.style.visibility = "hidden";
                if (isSearch == 1) {
                    $("#dialog-message").dialog({
                        resizable: false,
                        height: 250,
                        width: 500,
                        modal: true,
                        buttons: {
                            Ok: function () {
                                $('.suggest').val('');
                                $('#btnDelete').css('display', 'none');
                                $(this).dialog("close");
                            }
                        }
                    });
                }
            }
        },
        error: function (msg) { alert('Connect to database is break. Please check connection info!'); }
    });
}

function BindMealPlan(response, userid, user) {
    var mealplan = response.d;
    var html = "";
    $('#leftpanel').html(html);
    html = "<ul class='ul02'>";
    for (var i = 0; i < mealplan.length; i++) {
        var varObjectID = mealplan[i].MealPlanID;
        var OldUser = mealplan[i].UserID;
        var varObjectName;
        var varObjectNameFull;
        if (mealplan[i].MealPlanName != null) {
            varObjectName = mealplan[i].MealPlanName.substring(0, 10);

            varObjectNameFull = mealplan[i].MealPlanName;
            if (varObjectNameFull.length > 10) varObjectName = varObjectName + ' ..';
        }
        else {
            varObjectName = 'No Name..';
            varObjectNameFull = 'No Name';
        }

        var varObjectImage = mealplan[i].MealPlanPhoto;
        var RecipeList = mealplan[i].ListRecipeId;
        var varUserName = mealplan[i].UserName;
        var rating = mealplan[i].MealPlanRating;
        var description = mealplan[i].Description;
        var imageLink = mealplan[i].ImageLink;
        if (imageLink == '') imageLink = 'images/PlanItMeal_NoImage.png';
        var image = mealplan[i].MealPlanPhoto;
        if (image == '') image = 'images/PlanItMeal_NoImage.png';

        rating = "'" + rating + "'";
        description = description.replace('"', '').replace('"', '').replace('"', '').replace('"', '');
        varObjectName = varObjectName.replace('"', '').replace('"', '').replace('"', '').replace('"', '');
        varObjectNameFull = varObjectNameFull.replace('"', '').replace('"', '').replace('"', '').replace('"', '');


        if (varObjectImage != null) {
            html += '<li id="' + varObjectID + '">';
            html += '<p class="text" onclick="javascript:RequestMealPlanDetail(' + varObjectID + ')">' + varObjectNameFull + '</a></p>';
            html += '<p class="mar_b04"><a><img class="tooltip" src="' + image + '" OnError="this.src=\'images/PlanItMeal_NoImage.png\';" alt="" width="220" height="220" title="<b>Name: </b>' + varObjectNameFull + '</br><b>Description: </b>' + description + '</br><b>Author: </b>' + varUserName + '</br><div class=basic data-average=' + rating + ' data-id=1></div>" ondblclick="javascript:RequestMealPlanDetail(' + varObjectID + ')"/></a></p>';
            html += '<div class="clearfix">';
            if (userid == '0') {
                if (user != '')
                    html += '<p class="img_l"><img title="Add to my meal plan" onclick="javascript:AddToMyMealPlan(' + varObjectID + ',' + user + ',' + OldUser + ')" id="staricon" src="images/icon1.jpg" alt="" width="13" height="12" /></p>';
            }
            html += '<p class="img_r"><img title="Add to grocery list" id="plusicon" src="images/icon2.jpg" alt="" width="13" height="13" /></p>';
            html += '</div>';
            html += '<div id="RecipeList' + varObjectID + '" style="display:none">' + RecipeList + '</div>';
            html += '</li>';    
        }
    }

    //    html += "</div>";

    html += '</ul>';
    $('#leftpanel').append(html);
    $('#loading').hide();
    var divbg = document.getElementById('bg');
    divbg.style.visibility = "hidden";

    init_tooltip();
    dropdragmealplan();
}

function AddToMyMealPlan(MealPlanId, UserId, OldUser) {
    $("#" + MealPlanId).remove();
    $.ajax({
        type: "POST",
        url: "PIMService.asmx/InsertMyMealPlan",
        data: "{'_mealPlanId':'" + MealPlanId + "','_userId':'" + UserId + "','_oldUser':'" + OldUser + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            result = response.d;
            if (result == "OK") {
            }
            else {
            }
        },
        error: function (msg) {
        }
    });


}

function RequestMealPlanDetail(varObjectID) {
    self.location = "MealPlanDetails.aspx?MealPlanId=" + varObjectID;
}

function BindMealPlanSuggestion() {

    $.ajax({
        type: "POST",
        url: "PIMService.asmx/GetMealPlan",
        data: "{'MealPlanId':'0','UserId':'0'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var availableTags = '';
            var object = response.d;
            for (var i = 0; i < object.length; i++) {
                var varObjectNameFull;
                varObjectNameFull = object[i].MealPlanName;

                var Sugges = varObjectNameFull;
                availableTags += Sugges + ",";
            }
            availableTags = availableTags.substring(0, availableTags.length - 1);

            var suggestion = availableTags.split(',');
            $(".suggest").autocomplete({
                source: suggestion
            });
        }
    });
}